package NBU::Host;

require 5.002;

use strict;
use Carp;

my %hostList;

BEGIN {
  use Exporter   ();
  use AutoLoader qw(AUTOLOAD);
  use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
  $VERSION =     '0.01';
  @ISA =         qw();
  @EXPORT =      qw();
  @EXPORT_OK =   qw();
  %EXPORT_TAGS = qw();
}

sub new {
  my $Class = shift;
  my $host = {
  };

  bless $host, $Class;

  if (@_) {
    $host->{NAME} = shift;
    $hostList{$host->{NAME}} = $host;
  }
  return $host;
}

sub populate {
  my $Class = shift;

  open(PIPE, $NBU::prefix."netbackup/bin/admincmd/bpclclients -allunique -noheader |");
  while (<PIPE>) {
    my ($platform, $os, $name) = split;
    my $host = NBU::Host->byName($name);

    $host->os($os);
    $host->platform($platform);
  }
  close(PIPE);
}

sub byName {
  my $Class = shift;
  my $name = shift;

  if (my $host = $hostList{$name}) {
    return $host;
  }
  else {
    return NBU::Host->new($name);
  }
}

sub hostList {
  my $Class = shift;

  return (values %hostList);
}

sub loadClass {
  my $self = shift;
  my $newClass = shift;

  if (!defined($self->{CLASSES})) {
    $self->{CLASSES} = [];
  }

  my $classListR = $self->{CLASSES};
  push @$classListR, $newClass;

  return $newClass;
}

sub classList {
  my $self = shift;

  my $classListR = $self->{CLASSES};
  if (defined($classListR)) {
    return (@$classListR);
  }
  return undef;
}

sub name {
  my $self = shift;

  return $self->{NAME};
}

sub platform {
  my $self = shift;

  if (@_) {
    $self->{PLATFORM} = shift;
  }

  return $self->{PLATFORM};
}

sub os {
  my $self = shift;

  if (@_) {
    $self->{OS} = shift;
  }

  return $self->{OS};
}

sub loadCoverage {
  my $self = shift;
  my $name = $self->name;

  open(PIPE, $NBU::prefix."netbackup/bin/admincmd/bpcoverage -c $name -no_cov_header -no_hw_header |");
#  open(PIPE, "<${name}.cov");

  my %coverage;
  my $loadOK;
  while (<PIPE>) {
    if (!$loadOK) {
      if (/^CLIENT: $name/) {
	while (<PIPE>) {
          last if (/Mount Point/ || /Drive Letter/);
	}
        $_ = <PIPE>;
        $loadOK = 1;
      }
    }
    elsif ($loadOK && !(/^$/) && !(/   Exit status/)) {
      my @remainder;
      my ($mountPoint, @remainder) = split;

      if ($self->os =~ /Solaris/) {
	my ($deviceFile, $className, $status) = @remainder;
        if ($className eq "UNCOVERED") {
	  $coverage{$mountPoint} = undef;
        }
        else {
	  $className =~ s/^\*//;
	  my $clR = $coverage{$mountPoint};
	  if (!$clR) {
	    $coverage{$mountPoint} = $clR = [];
	  }
	  my $class = NBU::Class->byName($className);
	  $class->providesCoverage(1);
          push @$clR, $class;
        }
      }
      elsif ($self->os =~ /Windows(NT|2000)/) {
        my ($className, $status) = @remainder;
        if ($className eq "UNCOVERED") {
	  $coverage{$mountPoint} = undef;
        }
        else {
	  $className =~ s/^\*//;
	  my $clR = $coverage{$mountPoint};
	  if (!$clR) {
	    $coverage{$mountPoint} = $clR = [];
	  }
	  my $class = NBU::Class->byName($className);
	  $class->providesCoverage(1);
          push @$clR, $class;
        }
      }
      else {
        print "coverage from ".$self->os.": $_";
      }
    }
    else {
      last;
    }
  }
  close(PIPE);

  $self->{COVERAGE} = \%coverage;
}

sub coverage {
  my $self = shift;

  if (!$self->{COVERAGE}) {
    $self->loadCoverage;
  }

  my $coverageR = $self->{COVERAGE};
  return (%$coverageR);
}
1;

__END__
